'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _arrayTreeFilter = require('array-tree-filter');

var _arrayTreeFilter2 = _interopRequireDefault(_arrayTreeFilter);

var _MultiPicker = require('../../m-picker/lib/MultiPicker.web');

var _MultiPicker2 = _interopRequireDefault(_MultiPicker);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var Cascader = _react2["default"].createClass({
    displayName: 'Cascader',
    getDefaultProps: function getDefaultProps() {
        return {
            cols: 3,
            prefixCls: 'rmc-cascader',
            pickerPrefixCls: 'rmc-picker',
            data: [],
            disabled: false
        };
    },
    getInitialState: function getInitialState() {
        return {
            value: this.getValue(this.props.data, this.props.defaultValue || this.props.value)
        };
    },
    componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
        if ('value' in nextProps) {
            this.setState({
                value: this.getValue(nextProps.data, nextProps.value)
            });
        }
    },
    onValueChange: function onValueChange(value, index) {
        var children = (0, _arrayTreeFilter2["default"])(this.props.data, function (c, level) {
            return level <= index && c.value === value[level];
        });
        var data = children[index];
        var i = void 0;
        for (i = index + 1; data && data.children && data.children.length && i < this.props.cols; i++) {
            data = data.children[0];
            value[i] = data.value;
        }
        value.length = i;
        if (!('value' in this.props)) {
            this.setState({
                value: value
            });
        }
        this.props.onChange(value);
    },
    getValue: function getValue(d, val) {
        var data = d || this.props.data;
        var value = val || this.props.value || this.props.defaultValue;
        if (!value || !value.length) {
            value = [];
            for (var i = 0; i < this.props.cols; i++) {
                if (data && data.length) {
                    value[i] = data[0].value;
                    data = data[0].children;
                }
            }
        }
        return value;
    },
    getCols: function getCols() {
        var _props = this.props,
            data = _props.data,
            cols = _props.cols;

        var value = this.state.value;
        var childrenTree = (0, _arrayTreeFilter2["default"])(data, function (c, level) {
            return c.value === value[level];
        }).map(function (c) {
            return c.children;
        });
        childrenTree.length = cols - 1;
        childrenTree.unshift(data);
        return childrenTree.map(function (children) {
            return {
                props: {
                    children: children || []
                }
            };
        });
    },
    render: function render() {
        var props = this.props;
        var prefixCls = props.prefixCls,
            pickerPrefixCls = props.pickerPrefixCls,
            className = props.className,
            rootNativeProps = props.rootNativeProps,
            disabled = props.disabled,
            pickerItemStyle = props.pickerItemStyle;

        return _react2["default"].createElement(_MultiPicker2["default"], { prefixCls: prefixCls, pickerPrefixCls: pickerPrefixCls, disabled: disabled, className: className, selectedValue: this.state.value, rootNativeProps: rootNativeProps, pickerItemStyle: pickerItemStyle, onValueChange: this.onValueChange }, this.getCols());
    }
});
exports["default"] = Cascader;
module.exports = exports['default'];